﻿//Ajax对象
var Ajax = Class.create();
Ajax.prototype = {
    req: null,
    options:{
        url:'?',
        method:       'post',
        async: true,
        contentType:  'application/x-www-form-urlencoded',
        encoding:     'UTF-8',
        params:   '',
        text: '正在加载中...',
        obj:window,
        args: null,
        loading: true,
        type: 'xml',
        onError: Ctn.util.emptyFn,
        onSuccess: Ctn.util.emptyFn,
        onComplete: Ctn.util.emptyFn,
        onStateChange: Ctn.util.emptyFn
    },
    _construct : function (option){
        this.req = Ctn.util.tryeach(
        function() {return new XMLHttpRequest()},
        function() {return new ActiveXObject('Msxml2.XMLHTTP')},
        function() {return new ActiveXObject('Microsoft.XMLHTTP')}
        );
    },
    request: function(opt){
        var o = this.options;
        Ctn.util.extend(o, opt);
        this.req.onreadystatechange = this.onStateChange.call(this);
        this.req.setRequestHeader("Content-Type", o.contentType + "; charset=" + o.encoding);
        this.req.open(o.url, o.method, o.async);
        this.req.send(o.params);
    },
    onSuccess: function(){
        var o = this.options;
        switch (o.type){
            case "xml": {
            }break;
            default: {
                o.onSuccess(this.req.responseText, o.args);
            }break;
        }
    },
    onError: function(){
    },
    onComplete:function(){
    },
    onStateChange: function(){
        var rs = this.req.readyState;
        if (rs == 4){
            var s = this.req.status;
            if(s == 200 || s == 0){
                this.onSuccess();
            }else{
                this.onError();
            }
        }
    }
}

Ctn.trans = {
    Ajax:Ajax,
    ajax: function(){
    },
    getText:function(){
    },
    getXml: function(){
    }
}